package com.globagames.isometric.core.model
{
	import com.globagames.isometric.core.view.IsoMap;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import org.cove.ape.AbstractParticle;
	
	
	public class SimpleObject implements IIsoObject
	{
		public var ox:Number;
		public var oy:Number;
		public var oz:int;
		
		public var ex:int;
		public var ey:int;
		public var ez:int;
		
		public var kx:int;
		public var ky:int;
		
		public var r:int;
		public var _depth:int;
		private var _bitmapData:BitmapData;
		private var _bitmapDataGhost:BitmapData;
		
		public var cases:Array;
		
		
		private var _label:String;	
		private var _identifier:String;	
		private var _visible:Boolean = false;
		public var style:int = 0;
		private var _particle:AbstractParticle;  
		  
		public function SimpleObject()
		{
			super();
			r = 30;
			ex = 2;
			ey = 2;
			ez = 3;
			ox = -1;
			oy = -1;
			cases = new Array(ey+1);
			for(var i:int = 0 ; i < ey+1 ; i++ ) {
				cases[i] = new Array(ex+1);
			}
			
		}
		public function clone():SimpleObject {
			var clone:SimpleObject = new SimpleObject();
			clone.ox = ox;
			clone.oy = oy;
			clone.oz = oz;
			clone.bitmapData = bitmapData;	
			clone.visible = true;		
			return clone;
		}
		public function get depth() : int {
			return _depth;
		}
		public function get label() : String {
			return _label;
		}
		public function get identifier() : String {
			return _identifier;
		}
		public function set identifier(id:String) : void {
			_identifier = id;
		}
		public function set label( label:String ) : void {
			_label = label;
		}
		public function get bitmapData() : BitmapData {
			return _bitmapData;
		}
		public function set bitmapData(bitmapData:BitmapData) : void {
			_bitmapData = bitmapData;
			
			_bitmapDataGhost = new BitmapData(bitmapData.rect.width, bitmapData.rect.height, true,0x00000000);	
			var last:uint = -1;
			var cPixel:uint = -1;
			var alphaColor:uint = 0x99222222;
			
			for(var h:int = 0; h < bitmapData.rect.height; h++) {
				for(var w:int = 0; w < bitmapData.rect.width; w++) {
					cPixel = bitmapData.getPixel32(w,h);
					if( (last == 0 && cPixel > 0) ) {
						bitmapDataGhost.setPixel32(w,h, alphaColor);
						
					} else 	if( last > 0 && cPixel == 0) {
						bitmapDataGhost.setPixel32(w-1,h, alphaColor);
					} else {
						if( bitmapData.getPixel32(w,h-1) == 0 && cPixel > 0) {
							bitmapDataGhost.setPixel32(w,h, alphaColor);	
						}else if( bitmapData.getPixel32(w,h+1) == 0 && cPixel > 0) {
							bitmapDataGhost.setPixel32(w,h, alphaColor);	
						}
					} 
					last = cPixel;
				}	
			}
		}
		public function get bitmapDataGhost() : BitmapData {
			return _bitmapDataGhost;	
		}
		public function set bitmapDataGhost(bitmapDataGhost:BitmapData) : void {
			//_bitmapDataGhost = bitmapData.clone();
		}
		public function removeFomMap():void {
			if( ox > -1 ) {
				for(var yy:int = 0 ; yy <= ey ; yy++ ) {
					for(var xx:int = 0 ; xx <= ex  ; xx++ ) {
						var tileSrc:IsoTile = cases[yy][xx] as IsoTile;
						if( tileSrc ) {
							tileSrc.removeReference(this); 
							cases[yy][xx] = null;
						}					
					}
				}
				ox = -1;
				oy = -1;	
				_depth = -1;
			}
		}
		
		public function computeBitmapRectangle(tile:IsoTile):Array {
			var ret:Array = null;
			var px:int = ox;
			var py:int = oy;
			var px3d = px - py;
			var py3d = Math.floor(( px + py )/2);
			px3d -= 32;				
			var h:int = bitmapData.rect.height;			
			var diff:int = px3d - tile.x3d;
			var rect:Rectangle = new Rectangle(0,0, 128, h);
			var p:Point = new Point(diff,py3d - tile.y3d );
			ret = new Array();
			ret.push(p);ret.push(rect); 
			return ret;
		}
		public function computeBitmapRectangleForDisplay(isomap:IsoMap, before:Boolean, tile:IsoTile):Array {
			var ret:Array = null;			
			var px:int = ox;
			var py:int = oy;
			var px3d = px - py;
			var py3d = Math.floor(( px + py )/2);
			px3d -= 32;				
			var h:int = bitmapData.rect.height;
			var hit:IsoTile = isomap.testIsoTile(ox,oy);
			hit = isomap.findIsoTile(hit.x2d-1,hit.y2d+1);
			if( hit && hit == tile  && ( (before && hit._depth >_depth )||(!before && hit._depth <= _depth) )) {
				var diff:int = px3d - tile.x3d;
				var rect:Rectangle = new Rectangle(0,0, 128, h);
				var p:Point = new Point(diff,py3d - tile.y3d );
				ret = new Array();
				ret.push(p);ret.push(rect);
			} 			
			return ret;
		}
	
		
		
		public function get visible() : Boolean {
			return _visible;
		}
		public function set visible(value:Boolean) : void {
			_visible = value;
		}
		
		public function get particle() : AbstractParticle {
			return _particle;
		}
		public function set particle(p:AbstractParticle) : void {
			_particle = p;
		}
		

	}
	
	
}